<?php
class ControllerProductProduct extends Controller
{
	public function __construct()
	{
		$this->id="productlist";
		//$this->iscache=true;
	}

	public function index()
	{
		$mediaid = $this->request->get['mediaid'];
		if($mediaid == "")
		{
			$this->getList();
		}
		else
		{
			$this->getForm();
		}
	}
	
	private function getList()
	{
		$this->load->model("core/media");
		$this->load->model("core/sitemap");
		$this->load->helper('image');
		
		$sitemapid = $this->document->sitemapid;
		$mediaid = $this->request->get['mediaid'];
		$siteid = $this->user->getSiteId();
		$keyword = $this->request->get['keyword'];
		$step = (int)$this->request->get['step'];
		$to = 15;
		
		//Get list
		$this->data['haspage'] = true;
		$queryoptions = array();
		$queryoptions['mediaparent'] = '%';
		$queryoptions['mediatype'] = 'product';

		if($sitemapid == "")
		{
			$queryoptions['refersitemap'] = "%";
			$this->data['haspage'] = false;
		}
		else
		{
			$listsitemapid = array();
			$this->model_core_sitemap->getTreeSitemap($sitemapid, $listsitemapid, $this->user->getSiteId());
			$list = $this->string->matrixToArray($listsitemapid,"sitemapid");
			$queryoptions['refersitemap'] = $list;
		}
		$queryoptions['keyword'] = '%';
		
		if($keyword != "")
			$queryoptions['keyword'] = $keyword;
		if($mediaid == "")
		{
			$medias = $this->model_core_media->getPaginationList($queryoptions, $step, $to);
			
			$this->data['medias'] = array();
			
		
			$index = -1;
			foreach($medias as $media)
			{
				$index += 1;
				$s =  str_replace("[","",  $media['refersitemap']);
				$arr = split("]",$s);
				
				$link = HTTP_SERVER."index.php?route=page/detail&sitemapid=".$arr[0]."&mediaid=".$media['mediaid'];
				
				$imagethumbnail = "";
				if($media['imagepath'] != "")
				{
					/*<a href="#"><img src="<?php echo $media['imagethumbnail']?>" class="ben-left" /></a>*/
					$imagethumbnail_source = HelperImage::fixsize($media['imagepath'], 160, 120);
					$imagethumbnail = "<a href='".$link."' data-tooltip='sticky".$media['mediaid']."'><img src='".HelperImage::resizePNG($media['imagepath'], 160, 120)."' class='item-image' /></a>";
					$imagepreview = "<a href='".$link."'><img src='".HelperImage::resizePNG($media['imagepath'], 300, 220)."' class='item-image' /></a>";
				}
				else
				{
					$imagethumbnail_source = HelperImage::resizePNG("../".DIR_IMAGE."noimage.gif", 180, 180);
					$imagethumbnail = "<a href='".$link."'><img src='".HelperImage::resizePNG("../".DIR_IMAGE."noimage.gif", 177, 157)."' class='item-image' /></a>";
					$imagepreview = "<a href='".$link."'><img src='".HelperImage::resizePNG("../".DIR_IMAGE."noimage.gif", 300, 220)."' class='item-image' /></a>";
				}
	
				
				$this->data['medias'][] = array(
					'mediaid' => $media['mediaid'],
					'title' => $media['title'],
					'summary' => $media['summary'],
					'imagethumbnail_source' => $imagethumbnail_source,
					'imagethumbnail' => $imagethumbnail,
					'imagepreview' => $imagepreview,
					'statusdate' => $this->date->formatMySQLDate($media['statusdate'], 'longdate', "/"),
					'link' => $link
				);
				
			}
			
			$querystring = "index.php?route=page/detail&sitemapid=".$sitemapid;
			//$querystring = $this->pager->getURLQueryString("step", 0);
			$pagelinks = $this->model_core_media->getPaginationLinks($index, $queryoptions, $querystring, $step, $to);
			
			$this->data['nextlink'] = $pagelinks['nextlink'];
			$this->data['prevlink'] = $pagelinks['prevlink'];
		}
		//print_r($this->data['medias']);
		$this->id="productlist";
		$this->template="product/product.tpl";
		$this->render();
	}
	
	private function getForm()
	{
		$this->load->model("core/media");
		$this->load->helper('image');
		
		$sitemapid = $this->document->sitemapid;
		$mediaid = $this->request->get['mediaid'];
		$siteid = $this->user->getSiteId();
		
		$sitemap = $this->model_core_sitemap->getItem($this->document->sitemapid, $siteid);
		$this->data['post'] = $this->model_core_media->getItem($mediaid);
		
		if(count($this->data['post']) == 0)
		{
			$this->data['post']['description'] = "Updating...";
		}
		
		$this->data['post']['link'] = "index.php?route=page/detail&sitemapid=".$sitemapid."&mediaid=".$mediaid;
		$this->data['post']['description'] = html_entity_decode($this->data['post']['description']);
		
		if($this->data['post']['imagepath'] != "")
		{
			$imagepreview = HelperImage::resizePNG($this->data['post']['imagepath'], 600, 400);
			$this->data['post']['imagethumbnail_source'] = HelperImage::resizePNG($this->data['post']['imagepath'], 180, 180);
			$this->data['post']['imagethumbnail'] = "<a rel='lightbox[".$mediaid."]' href='".$imagepreview."'><img class='png' src='".$this->data['post']['imagethumbnail_source']."' class='ben-alignleft' /></a>";
		}
		else
		{
			$this->data['post']['imagethumbnail_source'] = HelperImage::resizePNG("../".DIR_IMAGE."noimage.gif", 180, 180);
			$this->data['post']['imagethumbnail'] = "<img src='".$this->data['post']['imagethumbnail_source']."' class='ben-alignleft' />";
			
		}
		//Get list sub image
		$attachment=$this->model_core_media->getInformation($mediaid, "attachment");
		$this->data['post']['subimage'] = array();
		if($attachment != "")
		{
			$listfile = split(",",$attachment);
			foreach($listfile as $key => $item)
			{
				$file = $this->model_core_media->getFile($item);
				$imagepreview = HelperImage::resizePNG($file['filepath'], 600, 400);
				$imagethumbnail = "<a rel='lightbox[".$mediaid."]' href='".$imagepreview."'><img class='png' src='".HelperImage::resizePNG($file['filepath'], 180, 180)."' class='ben-alignleft' /></a>";
				
				$this->data['post']['subimage'][] = $imagethumbnail;
				//$this->data['post']['subimage'][$key]['imageprivew'] = $imageprivew;
			}
		}
		
		//Other Product
		$queryoptions = array();
		$queryoptions['mediaparent'] = '%';
		$queryoptions['mediatype'] = '%';
		$queryoptions['refersitemap'] = $sitemapid;
		$queryoptions['date'] = $this->data['post']['statusdate'];
		$otherproducts = $this->model_core_media->getOthersMedia($mediaid, $queryoptions, 9);
		
		
		$this->data['medias'] = array();
		$index = -1;
		
		
		foreach($otherproducts as $media)
		{
			$index += 1;
			$s =  str_replace("[","",  $media['refersitemap']);
			$arr = split("]",$s);
			
			$link = HTTP_SERVER."index.php?route=page/detail&sitemapid=".$arr[0]."&mediaid=".$media['mediaid'];
			
			$imagethumbnail = "";
			$imagepreview = "";
			if($media['imagepath'] != "")
			{
				/*<a href="#"><img src="<?php echo $media['imagethumbnail']?>" class="ben-left" /></a>*/
				$imagethumbnail_source = HelperImage::fixsize($media['imagepath'], 160, 120);
				$imagethumbnail = "<a href='".$link."'><img class='png' data-tooltip='sticky".$media['mediaid']."' src='".$imagethumbnail_source."' class='item-image' /></a>";
				$imagepreview = "<a href='".$link."'><img class='png' src='".HelperImage::resizePNG($media['imagepath'], 300, 220)."' class='item-image' /></a>";
			}
			else
			{
				$imagethumbnail_source = HelperImage::fixsize("../".DIR_IMAGE."noimage.gif", 160, 120);
				$imagethumbnail = "<a href='".$link."'><img src='".$imagethumbnail_source."' class='item-image' /></a>";
				$imagepreview = "<a href='".$link."'><img src='".HelperImage::resizePNG("../".DIR_IMAGE."noimage.gif", 300, 220)."' class='item-image' /></a>";
			}

			
			$this->data['medias'][] = array(
				'mediaid' => $media['mediaid'],
				'title' => $media['title'],
				'summary' => $media['summary'],
				'imagethumbnail_source' => $imagethumbnail_source,
				'imagethumbnail' => $imagethumbnail,
				'imagepreview' => $imagepreview,
				'statusdate' => $this->date->formatMySQLDate($media['statusdate'], 'longdate', "/"),
				'link' => $link
			);
		}
		
		
		$this->id="news";
		$this->template="product/productdetail.tpl";
		$this->render();
	}
}
?>